package org.lyd.spring.tx.dao;

import org.lyd.spring.tx.bean.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

/**
 * @Author:lyd
 **/
@Component
public class BookDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    //安装id查询图书
    public Book queryBookById(Integer id){
        String sql = "select * from book where id = ?";
        Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id);
        return book;
    }

    //新增一本图书
    public void addBook(Book book){
        String sql = "insert into book(bookName,price,stock) values(?,?,?)";
        jdbcTemplate.update(sql,book.getBookName(),book.getPrice(),book.getStock());
    }
    //根据id删除图书数量
    public void updateBook(Integer id, Integer num){
        String sql = "update book set stock = stock - ? where id = ?";
        jdbcTemplate.update(sql,num,id);
    }


}
